// request.js
const BASE_URL = 'http://localhost:3000'; // 你的API基础URL

function request(options) {
    // 默认配置
    const defaultOptions = {
        url: '',
        method: 'GET',
        data: {},
        header: {
            'Content-Type': 'application/json'
        }
    };

    // 合并默认配置与传入的配置选项
    options = Object.assign({}, defaultOptions, options);

    return new Promise((resolve, reject) => {
        uni.request({
            ...options,
            url: BASE_URL + options.url, // 拼接基础URL和具体接口路径
            success: (response) => {
                if (response.statusCode === 200) {
                    resolve(response.data); // 成功时解析数据
                } else {
                    reject(`HTTP错误！状态码：${response.statusCode}`);
                }
            },
            fail: (error) => {
                reject(error); // 失败时抛出错误
            }
        });
    });
}

// 封装GET和POST方法以便于使用
export function get(url, data = {}) {
    return request({ url, method: 'GET', data });
}

export function post(url, data = {}) {
    return request({ url, method: 'POST', data });
}